using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Assignment2
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        private GraphicsDeviceManager graphics;
        private SpriteBatch spriteBatch;

        private SpriteFont font;

        private Matrix world;
        private Matrix view;
        private Matrix projection;

        private string text;

        private Vector2 screenCenter;
        private Vector2 stringShadow;
        private Vector2 stringPos;
        private Vector2 stringLength;

        private Vector3 cameraPosition;
        private Vector3 cameraTarget;
        private float fovAngle;
        private float aspectRatio;
        private float nearPlane;//near clipping plane distance
        private float farPlane;//far clipping plane distance

        private BasicEffect effect;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            IsMouseVisible = true;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here


            font = Content.Load<SpriteFont>("Arial");
            text = "RightClick: " + MouseHandler.CountMouse + "\nSpacebar: " + KeyboardHandler.CountSpacebar + "\nGamePad A: " + GamepadHandler.CountA;

            screenCenter = new Vector2(this.GraphicsDevice.Viewport.Width, this.GraphicsDevice.Viewport.Height) / 2;
            stringLength = font.MeasureString(text);

            stringPos = Vector2.Zero;

            fovAngle = MathHelper.ToRadians(45);
            aspectRatio = graphics.PreferredBackBufferWidth / graphics.PreferredBackBufferHeight;
            nearPlane = 1f; // the near clipping plane distance
            farPlane = 10000f; // the far clipping plane distance

            //cameraPosition = new Vector3(-stringPos.X, -stringPos.Y, 1000f);
            //cameraTarget = new Vector3(-stringPos.X, -stringPos.Y, 0f);
            cameraPosition = new Vector3(0, 0, 1000f);
            cameraTarget = new Vector3(0, 0, 0f);


            world = Matrix.CreateScale(1, -1, 1);
            view = Matrix.CreateLookAt(cameraPosition, cameraTarget, Vector3.Up);
            projection = Matrix.CreatePerspectiveFieldOfView(fovAngle, aspectRatio, nearPlane, farPlane);
            //projection = Matrix.CreateOrthographicOffCenter(0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, 0f, 100f);

            effect = new BasicEffect(GraphicsDevice);
            effect.TextureEnabled = true;
            effect.VertexColorEnabled = true;
            //effect.LightingEnabled = true;
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            if (Keyboard.GetState().IsKeyDown(Keys.Escape)) this.Exit();

            // TODO: Add your update logic here

            //refresh gamepad state
            GamepadHandler.InitGamepad();
            KeyboardHandler.InitKeyboard();
            MouseHandler.InitMouse();

            text = "RightClick: " + MouseHandler.CountMouse + "\nSpacebar: " + KeyboardHandler.CountSpacebar + "\nGamePad A: " + GamepadHandler.CountA;

            //count of click
            MouseHandler.RightClickListener();
            KeyboardHandler.SpacebarListener();
            GamepadHandler.ButtonAListener();

            //move listener
            MouseHandler.MouseDragListener(ref stringPos, ref stringLength);
            KeyboardHandler.KeyboardMoveListener(ref stringPos);
            GamepadHandler.GamePadMoveListener(ref stringPos);
            KeyboardHandler.KeyboardMoveListener(ref world);

            effect.World = world;
            effect.View = view;
            effect.Projection = projection;

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            //spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, view);
            spriteBatch.Begin(SpriteSortMode.Deferred, null, null, DepthStencilState.DepthRead, RasterizerState.CullNone, effect);
            for (int i = 0; i < 8; i++)
            {
                stringShadow = new Vector2(i);
                spriteBatch.DrawString(font, text, stringPos + stringShadow, new Color(255 - (8 - i) * 20, 255 - (8 - i) * 20, 255 - (8 - i) * 20), 0, stringLength / 2, 1f, SpriteEffects.None, 0);
            }
            //spriteBatch.DrawString(font, text, Vector2.Zero, Color.White, 0, stringLength / 2, 1f, SpriteEffects.None, 0);
            //spriteBatch.DrawString(font, Mouse.GetState().X + "  " + Mouse.GetState().Y + "  " + stringPos.X + "  " + stringPos.Y + "\n" + (stringPos+stringLength).X + " " + (stringPos+stringLength).Y, new Vector2(50, 50), Color.White);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
